<template>
  <div>
    <div>
      <!--卡片区域-->
      <el-card class="box-card">
        <!--标题及搜索区域-->
        <el-row type="flex">
          <el-col :span="4" :offset="3"></el-col>
          <el-col :span="8" :offset="3" justify=center>
            <span style="font-size: 30px"><h1>OAsystem论坛</h1></span>
          </el-col>
          <el-col :span="4" :offset="3"></el-col>
        </el-row>
        <el-row type="flex">
          <el-col :span="4" :offset="2"></el-col>
          <el-col :span="8" :offset="2" justify=center>
            <el-input placeholder="请输入内容" v-model="query.sousuo" clearable>
              <el-button slot="append" icon="el-icon-search" @click="getPLlist()"></el-button>
            </el-input>
          </el-col>
          <el-col :span="4" :offset="2"></el-col>
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="sendInfo" style="width: 100px"></el-button>
        </el-row>
      </el-card>
      <!--文章发布-->
      <el-dialog title="有什么重要的事分享给大家？" size="tiny" :visible.sync="dialogVisible" width="30%">
        <hr />
        <!--        主体区域-->
        <el-input type="textarea" style="margin-top: 10px;margin-bottom: 20px" v-model="contents.content"
                  :autosize="{ minRows: 4, maxRows: 4}" placeholder="请输入内容"></el-input>
          <el-tag :key="tag" v-for="tag in contents.dynamicTags" closable :disable-transitions="false" @close="handleClose(tag)">
          {{tag}}
        </el-tag>
        <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small"
                  @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm"></el-input>
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新建标签</el-button>

        <!--        底部区域-->
        <span slot="footer" class="dialog-footer">
<!--          <el-button @click="dialogVisible = false">取 消</el-button>-->
          <el-button type="primary" @click="forum()">发 布</el-button>
        </span>
      </el-dialog>
    </div>
    <div>
      <el-card class="box-card1">
        <div v-for="item in plist" :key="item.comment" style="margin-bottom: 50px">
          <!--          <img src="{{ item.publisher }}">-->
          <p>
            <span style="font-size: 10px"><el-button type="text" style="font-size: 20px">{{ item.publisher }}</el-button>  | 发布于 {{ item.commenttime }}</span>
          </p>
          <p><el-tag :key="tag" size="mini" v-for="tag in contents.dynamicTags">{{ tag }}</el-tag></p>
          <p style="margin: 20px">{{ item.comment }}</p>
          <div style="margin-top: 20px">
            <i class="iconfont icon-dianzan" style="cursor: pointer" @click="dianzan"></i>
<!--            <span style="margin: 20px">|</span>-->
            <i class="el-icon-chat-dot-round" style="cursor: pointer" @click="pinglun" id="pl"></i>
          </div>
          <hr>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      inputVisible: false,
      inputValue: '',
      plist: [],
      total: 0,
      dialogVisible: false,
      query: {
        sousuo: ''
      },
      contents: {
        content: '',
        dynamicTags: []
      }
    }
  },
  created () {
    this.getPLlist()
  },
  methods: {
    // 获取文章
    async getPLlist () {
      const { data: rsp } = await this.$http.get('/forum', { params: this.query })
      if (rsp.status !== 200) {
        return this.$message.error('数据获取失败')
      }
      this.plist = rsp.data
      this.total = rsp.total
      console.log(rsp)
    },

    sendInfo () {
      this.dialogVisible = true
    },
    // 发布文章
    async forum () {
      const { data: rsp } = await this.$http.post('/forum')
      if (rsp.status !== 200) {
        return this.$message.error('发布失败')
      }
      this.dialogVisible = false
    },

    handleClose (tag) {
      this.contents.dynamicTags.splice(this.contents.dynamicTags.indexOf(tag), 1)
    },

    showInput () {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm () {
      const inputValue = this.inputValue
      if (inputValue) {
        this.contents.dynamicTags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    // 点赞
    dianzan () {
      window.alert('点赞')
    },
    // 评论
    pinglun () {
      window.alert('评论')
    }
  }
}
</script>

<style scoped>
  .box-card1 {
    margin-top: 10px;
  }

  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }

  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
  #pl {
    margin-left: 50px;
  }
</style>
